using System;
using System.Reflection;
using UnityEngine;

namespace NodeCanvas.Variables
{
	public abstract class VariableData : MonoBehaviour
	{
		public string dataName;

		private Type _type;

		private FieldInfo _valueField;

		private object currentValue;

		public new string name
		{
			get
			{
				return dataName;
			}
		}

		public virtual Type varType
		{
			get
			{
				if (_type == null)
				{
					_type = GetType().NCGetField("value").FieldType;
				}
				return _type;
			}
		}

		public virtual object objectValue
		{
			get
			{
				if (_valueField == null)
				{
					_valueField = GetType().NCGetField("value");
				}
				return _valueField.GetValue(this);
			}
			set
			{
				if (currentValue != value)
				{
					if (_valueField == null)
					{
						_valueField = GetType().NCGetField("value");
					}
					_valueField.SetValue(this, value);
					currentValue = value;
					OnValueChanged(value);
				}
			}
		}

		public event Action<string, object> onValueChanged;

		protected void OnValueChanged(object value)
		{
			if (this.onValueChanged != null)
			{
				this.onValueChanged(dataName, value);
			}
		}

		public virtual object GetSerialized()
		{
			return objectValue;
		}

		public virtual void SetSerialized(object obj)
		{
			objectValue = obj;
		}

		public override string ToString()
		{
			return (objectValue == null) ? "NULL" : objectValue.ToString();
		}
	}
}
